package com.longlong.test;

import com.longlong.mapper.UserMapper;
import com.longlong.pojo.Users;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

/**
 * @Author DELL longlong
 * @Date 2022/7/3 19:32
 * @Version 1.0
 * @Function 测试类
 */
public class Mytest {
   SqlSession sqlSession;
   //动态代理的对象
   UserMapper userMapper;
   //日期格式化刷子
   SimpleDateFormat sf =  new SimpleDateFormat("yyyy-MM-dd");
   @Before
    public void openSqlSession() throws IOException {
       //1.读取核心配置文件按
       InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");

       //2.创建工厂对象
       SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

       //3.取出sqlsession
       sqlSession = factory.openSession();
      //取出动态代理的对象，完成接口中方法的调用，实则是调用xml文件中相应的标签功能
      userMapper = sqlSession.getMapper(UserMapper.class);
   }

   @After
    public void closeSqlSession(){
       sqlSession.close();
   }
   @Test
    public void testGetAll(){

       System.out.println(userMapper);
       List<Users> list = userMapper.getAll();
       list.forEach(users -> System.out.println(users));
   }
   @Test
   public void testUpdate() throws ParseException {
      Users users = new Users(7,"haha666",sf.parse("2000-01-01"),"2","河南");
      int num = userMapper.update(users);
      System.out.println(num);
      sqlSession.commit();
   }

   @Test
   public void testGetById(){
      Users u = userMapper.getById(1);
      System.out.println(u);
   }
   @Test
   public void testGetByName(){
     List<Users> list = userMapper.getByName("小");
     list.forEach(users -> System.out.println(users));
   }
   @Test
   public void testGetByNameGood(){
      List<Users> list = userMapper.getByName("小");
      list.forEach(users -> System.out.println(users));
   }

   @Test
   public void testGetByNameOrAddress(){
      List<Users> list = userMapper.getByNameOrAddress("address","市");
      list.forEach(users -> System.out.println(users));
   }

   @Test
   public void testAdd() throws ParseException {
      Users u = new Users("hahahah66",sf.parse("2022-11-12"),"1","河南竹马带你");
      int num = userMapper.add(u);
      System.out.println(num);
      System.out.println(u);
      sqlSession.commit();
   }
   @Test
   public void testDelete(){
      int num = userMapper.delete(1);
      System.out.println(num);
      sqlSession.commit();
   }

   @Test
   public void testUUID(){
      UUID uuid = UUID.randomUUID();
      System.out.println(uuid.toString().replace("-",""));
   }

   @Test
   public void testGetByCondition(){
      Users u = new Users();
      u.setSex("1");
      u.setAddress("市");
      List<Users> list = userMapper.getByCondition(u);
      list.forEach(users -> System.out.println(users));
   }
}
